//
//  PayListRequestModel.m
//  ExcellentHome
//
//  Created by 揭婧雯 on 16/8/23.
//  Copyright © 2016年 陈区. All rights reserved.
//

#import "PayListRequestModel.h"


@implementation PayListRequestModel
/**
 *  提交支付信息
 *
 *  @param finished 网络请求完成后的操作
 */
- (void)requesConfirmBillPayWithDictionary:(NSDictionary*)params finished:(SEGNetworkReponse)finished
{
    NSString* url = [[params objectForKey:@"URL"] copy];
    
    //网络请求地址不需要传给后台
    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:params];
    [dict removeObjectForKey:@"URL"];
    
    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:url parameters:dict finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        finished(isSuccessed,result,codeType, errorMessage);
    }];
}

/**
 *   提交请求结果
 *
 *  @param requestId
 *  @param finished 网络请求完成后的操作
 */
- (void)requestPollByPaymenetResultlWithRequestId:(NSString*)requestId finished:(SEGNetworkRequestCallBack)finished
{
    NSDictionary* params = [NSDictionary dictionaryWithObjectsAndKeys:requestId, @"requestId", nil];
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/uhomecp-app/rest-api/v1/payment/pollByPaymenetResult.json",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:GET URLString:urlString parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        finished(result, nil);
    }];
}

/**
 *   检测是否包含已缴费的账单－－购物
 *
 *  @param orderId
 *  @param finished 网络请求完成后的操作
 */
- (void)requestCheckOrderStatusWithOrderId:(NSString*)orderId finished:(SEGNetworkReponse)finished
{
    NSDictionary* params = [NSDictionary dictionaryWithObjectsAndKeys:orderId, @"orderId", @"2", @"site", nil];
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/uhomecp-app/rest-api/v1/payment/checkOrderPaymentStatus.json",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:urlString parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        if (!isSuccessed) {
            finished(isSuccessed,result,codeType,errorMessage);
            return;
        }
        
        NSDictionary* params = [NSDictionary dictionaryWithObjectsAndKeys:@"2", @"site", nil];
        NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/uhomecp-app/rest-api/v1/payment/payMethodList.json",nil];
        [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:urlString parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
            if (!isSuccessed) {
                finished(isSuccessed,result,codeType,errorMessage);
                return;
            }
            [self requestPayMethodListWithRequestId:[result objectForKey:@"data"] finished:^(id result, NSError* error) {
                finished(isSuccessed,result,codeType,errorMessage);
            }];
        }];
    }];
}

- (void)requestPayMethodListWithRequestId:(NSString*)requestId finished:(SEGNetworkRequestCallBack)finished
{

    NSDictionary* params = [NSDictionary dictionaryWithObjectsAndKeys:requestId, @"requestId", nil];
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/uhomecp-app/rest-api/v1/payment/pollByPayMethodResult.json",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:urlString parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        
        if ([codeType isEqualToString:@"0"]) {
            
            NSMutableDictionary* data = [result objectForKey:@"data"];
            NSArray* mentodInfos = [data objectForKey:@"mentodInfos"];
            
            NSMutableArray* objectArray = [NSMutableArray array];
            for (NSDictionary* dic in mentodInfos) {
                NSMutableDictionary* retDic = [NSMutableDictionary dictionary];
                
                [retDic setObject:[data objectForKey:@"payeeId"] forKey:@"payeeId"];
                [retDic setObject:[dic objectForKey:@"id"] forKey:@"id"];
                [retDic setObject:[dic objectForKey:@"name"] forKey:@"name"];
                [retDic setObject:[dic objectForKey:@"forwardUrl"] forKey:@"forwardUrl"];
                [retDic setObject:[dic objectForKey:@"iconUrl"] forKey:@"iconUrl"];
                
                [objectArray addObject:retDic];
            }
            self.payMethodList = objectArray;
            finished(result, nil);
        }
        else { // 数据异常
            if (requestCount < 10) { // 请求次数小于10 继续请求
                requestCount++;
                [NSThread sleepForTimeInterval:1.0f];
                
                [self requestPayMethodListWithRequestId:requestId finished:^(id result, NSError* error) {
                    finished(result, nil);
                }];
            }
            else { // 异常处理
                finished(result, nil);
            }
        }
        finished(result, nil);
    }];
}

/**
 *  提交支付信息列表--->购物
 *
 *  @param orderId
 *  @param finished 网络请求完成后的操作
 */
- (void)requestSubmitOrderPaylistWithOrderId:(NSString*)orderId withPayMethod:(NSString*)payMethod withPayeeld:(NSString*)payeeId withUrl:(NSString*)url finished:(SEGNetworkReponse)finished {

    NSDictionary* params = [NSDictionary dictionaryWithObjectsAndKeys:orderId, @"orderId", payMethod, @"payMethod", payeeId, @"payeeId", url, @"forwardUrl", nil];
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/uhomecp-app/rest-api/v1/payment/submitOrderPay.json",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:urlString parameters:params finished:finished];
}

/**
 *  H5抽奖判断
 *  @param finished 网络请求完成后的操作
 */
- (void)requestCheckActivityWithFrom:(NSString*)from finished:(SEGNetworkRequestCallBack)finished
{
    NSDictionary* params = [NSDictionary dictionaryWithObjectsAndKeys:from, @"from", nil];
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/uhomecp-app/ecomm/activity/checkActivity.json",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:urlString parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        finished(result, nil);
    }];
}

/**
 * 月租车位支付成功回调
 * @author Wendy
 * @param MID 月卡唯一标识
 * @param OrderNo 业务流水号
 * @param PayNo 支付单号
 * @param PayType 支付类型 (0：现金 1：微信支付 2：支付宝支付 3：银联支付 4：ETC支付 5：招商通支付 6：其它支付)
 * @param PayTime 支付时间
 * @param Total 金额
 * @param finished 回调结果
 */
+ (void)requestMonthCardSavePayRecordWithMID:(NSString *)MID
                                     OrderNo:(NSString *)OrderNo
                                       PayNo:(NSString *)PayNo
                                     PayType:(NSString *)PayType
                                     PayTime:(NSString *)PayTime
                                       Total:(NSString *)Total
                                    finished:(SEGRequestCallBack)finished
{
    NSDictionary *params = @{
                             @"MID":MID,
                             @"OrderNo":OrderNo,
                             @"PayNo":PayNo,
                             @"PayType":PayType,
                             @"PayTime":PayTime,
                             @"Total":Total,
                             @"Token":@""
                             };

    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/uhomecp-app/api/MonthCard/SavePayRecord",nil];
    [SEGHTTPBusinessManager seg_requestMerchantWithMethod:POST URLString:urlString parameters:params dataClass:[NSDictionary class] finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        if (isSuccessed) {
            
        }
        finished(isSuccessed ,codeType ,errorMessage);
    }];
}

/**
 * 车辆临停 获取App返回的支付结果
 * @author Wendy
 * @param PID 停车场ID
 * @param OrderNo 业务流水号
 * @param PlateNum 车牌号
 * @param PayNo 支付单号
 * @param PayType 支付类型 (0：现金 1：微信支付 2：支付宝支付 3：银联支付 4：ETC支付 5：招商通支付 6：其它支付)
 * @param PayTime 支付时间
 * @param Coupon 优惠券（元）
 * @param Total 实收金额（元）
 * @param Receivable 应收账款（元）
 * @param CardNo 支付账号（卡号）
 * @param Operator 收费员（如：达实/招商通/停车场保安）
 * @param finished 回调结果
 */
+ (void)requestSavePayRecordWithPlatePID:(NSString *)PID
                                 OrderNo:(NSString *)OrderNo
                                PlateNum:(NSString *)PlateNum
                                   PayNo:(NSString *)PayNo
                                 PayType:(NSString *)PayType
                                 PayTime:(NSString *)PayTime
                                  Coupon:(NSString *)Coupon
                                   Total:(NSString *)Total
                              Receivable:(NSString *)Receivable
                                  CardNo:(NSString *)CardNo
                                Operator:(NSString *)Operator
                                finished:(SEGRequestCallBack)finished
{
    NSDictionary *params = @{
                             @"PID":PID,
                             @"OrderNo":OrderNo,
                             @"PlateNum":PlateNum,
                             @"PayNo":PayNo,
                             @"Token":@"",
                             @"PayType":PayType,
                             @"PayTime":PayTime,
                             @"Coupon":Coupon,
                             @"Total":Total,
                             @"Receivable":Receivable,
                             @"CardNo":CardNo,
                             @"Operator":Operator
                             };
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/uhomecp-app/api/Car/SavePayRecord",nil];
    [SEGHTTPBusinessManager seg_requestMerchantWithMethod:POST URLString:urlString parameters:params dataClass:[NSDictionary class] finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        if (isSuccessed) {
            
        }
        finished(isSuccessed ,codeType ,errorMessage);
    }];
}
@end
